﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.DAO;
using ClassLibrary.Entity;

public partial class Admin_ucControl_ucMenuChild : System.Web.UI.UserControl
{
    CategoryParrentDAO menuParrentDAO = new CategoryParrentDAO(Global.CONNECTION_STRING);
    CategoryChildDAO menuChildDAO = new CategoryChildDAO(Global.CONNECTION_STRING);

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            LoadMenu_Parrent();
            string idC = Request.QueryString["idC"];
            if (idC != "" || idC != null)
            {
                LoadDetailMenuChild();
            }
        }
    }
    // load menu Parrrent
    private void LoadMenu_Parrent()
    {
        reParentMenu.DataSource = menuParrentDAO.GetMenuParrent();
        reParentMenu.DataBind();
    }

    private void LoadDetailMenuChild()
    {
        string idC = Request.QueryString["idC"];
        CategoryChild menuChild = menuChildDAO.GetCateChild_ByIDChild(Global.ConvertToInt(idC));
        txtMenuChild.Text = menuChild.CateChildName;
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        string idP = Request.QueryString["idP"];
        string idC = Request.QueryString["idC"];
        string idPr = Request.QueryString["idPr"];

        bool flag = false;
        try
        {
            CategoryChild MenuChildren = new CategoryChild();
            MenuChildren.CateChildName = txtMenuChild.Text.Trim();
            MenuChildren.IDCateParrent = Global.ConvertToInt(idP);
            if (Global.ConvertToInt(idP) != 0)
            {
                menuChildDAO.InsUpCateChild(MenuChildren, 1);
                //return;
            }
            if (Global.ConvertToInt(idC) !=0)
            {
                MenuChildren.IDCateChild = Global.ConvertToInt(idC);

                MenuChildren.IDCateParrent = Global.ConvertToInt(idPr);

                menuChildDAO.InsUpCateChild(MenuChildren, 2);
               // return;
            }
            flag = true;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        if (flag)
        {
            Response.Redirect("~/Admin/MenuChildren.aspx");
        }
    }


    protected void reParentMenu_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Repeater reChildMenu = (Repeater)e.Item.FindControl("reChildMenu");
            HiddenField hidIDMenuP = (HiddenField)e.Item.FindControl("hidIDMenuP");
            hidIDMenuP.Value = DataBinder.Eval(e.Item.DataItem, "IDCateParrent").ToString();

            string idP = hidIDMenuP.Value;
          
            if (reChildMenu != null)
            {
                reChildMenu.DataSource = menuChildDAO.GetCateChild_ByIDParrent(Convert.ToInt32(idP));
                reChildMenu.DataBind();
            }
        }
    }
    protected void btnDel_Click(object sender, ImageClickEventArgs e)
    {
        bool flag = false;
        try
        {
            CategoryChild MenuChild = new CategoryChild();
            int idDelete = Convert.ToInt32(((ImageButton)sender).CommandArgument);
            menuChildDAO.DeleteMenuChild(idDelete);
            flag = true;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        if (flag)
        {
            Response.Redirect("~/Admin/MenuChildren.aspx");
        }
    }
}
